package com.syl.upimg.service.impl;

import com.syl.upimg.bean.TouTiaoBean;
import com.syl.upimg.enums.ImageUploadTypeEnum;
import com.syl.upimg.service.BaseUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * 今日头条羊毛
 *
 * @author syl
 * @create 2019-08-02 15:21
 **/
@Service
public class TouTiaoFleece implements BaseUpload {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public RestTemplate createRestTemplate() {
        return restTemplate;
    }

    @Override
    public String[] getLimit() {
        return new String[]{"jpg", "png", "gif"};
    }

    @Override
    public String getUrl() {
        return "https://mp.toutiao.com/upload_photo/?type=json";
    }

    @Override
    public HttpHeaders getDefaultHeader() {
        return this.getDefaultHeader("https://mp.toutiao.com", "https://mp.toutiao.com/profile_register_v3/register/register-normal/2");
    }

    @Override
    public LinkedMultiValueMap<String, Object> getParam(FileSystemResource file, String fileName, String fileType) {
        LinkedMultiValueMap<String, Object> linkedMultiValueMap = new LinkedMultiValueMap<>();
        linkedMultiValueMap.add("photo", file);
        return linkedMultiValueMap;
    }

    public TouTiaoBean upload(String filePath) throws IOException {
        return this.upload(filePath, false);
    }

    public TouTiaoBean upload(String filePath, boolean delete) throws IOException {
        TouTiaoBean upload = upload(filePath, TouTiaoBean.class, delete);
        upload.setBaseUrl(upload.getUrl()).setImageUploadTypeEnum(ImageUploadTypeEnum.TT);
        return upload;
    }

}
